<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];

	
		$t = @mysql_num_rows($r=mysql_query("SELECT * FROM `gg_users` where ref=0 or matrix_upline=0 group by name"));
		
		if($t<=0)
		{
			print_r("no record");
			exit;
		}
		
		$uid=0;
		$bal=0;
		$num=0;
		while($users = mysql_fetch_assoc($r))
		{
			//if($users['username']=='katsumori12'){
			$sponsor_id=0;
			if($users['ref']==0){
				$sponsor_id = @mysql_result(mysql_query("select ref from gg_users_20140429 where username='".str_replace("'","\'",$users['username'])."'"), 0) or 0; //print_r("select ref from gg_users_20140429 where username='".$users['username']."'<br>");
				if($sponsor_id>0){
					$sponsor_username = @mysql_result(mysql_query("select username from gg_users_20140429 where id=".$sponsor_id), 0) or 0; //print_r("select username from gg_users_20140429 where id=".$sponsor_id."<br>");
					$sponsor_id = @mysql_result(mysql_query("select id from gg_users where username='".str_replace("'","\'",$sponsor_username)."'"), 0) or 0; //print_r("select id from gg_users where username='".$sponsor_username."'<br>");
					mysql_query("UPDATE gg_users SET ref=".$sponsor_id." WHERE id=".$users['id']); //print_r("UPDATE gg_users SET ref=".$sponsor_id." WHERE id=".$users['id']);
					mysql_query("UPDATE gg_users SET ref=".$sponsor_id." WHERE main_uid=".$users['id']);
				}
			}
					
			$placement_id=0;
			if($users['matrix_upline']==0){
				$placement_id = @mysql_result(mysql_query("select matrix_upline from gg_users_20140429 where username='".str_replace("'","\'",$users['username'])."'"), 0) or 0; //print_r("select matrix_upline from gg_users_20140429 where username='".$users['username']."'<br>");
				if($placement_id>0){
					$placement_username = @mysql_result(mysql_query("select username from gg_users_20140429 where id=".$placement_id), 0) or 0; //print_r("select username from gg_users_20140429 where id=".$placement_id."<br>");
					if($users['matrix_position']=='1'){
						$placement_id = @mysql_result(mysql_query("select id from gg_users where username='".str_replace("'","\'",$placement_username)."-8'"), 0) or 0; //print_r("select id from gg_users where username='".$placement_username."-8'<br>");
					}else{
						$placement_id = @mysql_result(mysql_query("select id from gg_users where username='".str_replace("'","\'",$placement_username)."-15'"), 0) or 0; //print_r("select id from gg_users where username='".$placement_username."-15'<br>");
					}
					mysql_query("UPDATE gg_users SET matrix_upline=".$placement_id." WHERE id=".$users['id']); //print_r("UPDATE gg_users SET matrix_upline=".$placement_id." WHERE id=".$users['id']."<br>");
				}
			}
			//}
		}

	print_r("finish");
}
?>